<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true" label-width="100px">
      <el-form-item label="内部单号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入内部单号" clearable style="width: 240px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="线上订单号" prop="orderLineNo">
        <el-input v-model="queryParams.orderLineNo" placeholder="请输入线上订单号" clearable style="width: 240px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格数据 -->
    <el-table v-loading="loading" :scrollbar-always-on="true" :data="groupList">
      <el-table-column type="selection" width="55" />
      <el-table-column label="内部单号" prop="orderNo" width="200">
        <template #default="scope">
          <router-link :to="'/order/orderDetail?orderId=' + scope.row.id" class="link-type">
            <span>{{ scope.row.orderNo }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="商品" prop="goodsImageUrl" :show-overflow-tooltip="true">
        <template #default="scope">
          <el-image style="width: 100px; height: 100px" :src="scope.row.detailsList.goodsImageUrl" fit="cover" />
        </template>
      </el-table-column>
      <el-table-column label="件数" prop="orderTotalNum" :show-overflow-tooltip="true" width="50">
        <template #default="scope">
          <div style="display: flex;align-items: center;">
            {{scope.row.orderTotalNum}}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="订单备注" prop="remark" :show-overflow-tooltip="true" width="200" />
      <el-table-column label="买家留言" prop="orderBuyerMessage" :show-overflow-tooltip="true" width="200" />
      <el-table-column label="付款日期" prop="payTime" :show-overflow-tooltip="true" width="200" />
      <el-table-column label="付款金额" prop="payAmount" :show-overflow-tooltip="true" width="100"></el-table-column>
      <el-table-column label="店铺名称" align="center" width="100" prop="shopsName"></el-table-column>
      <el-table-column label="生产状态" align="center" width="200" prop="orderProduceStatus"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200" v-if="false">
        <template #default="scope">
          <el-tooltip content="一键复制租户" placement="top" v-if="scope.row.tenantGroupId !== 1">
            <el-button link type="primary" icon="Connection" @click="handleCopy(scope.row)" v-hasPermi="['gc:zhz:fz']"></el-button>
          </el-tooltip>
          <el-tooltip content="修改" placement="top" v-if="scope.row.tenantGroupId !== 1">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['gc:zhz:xg']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top" v-if="scope.row.tenantGroupId !== 1">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['gc:zhz:sc']"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script setup name="timeout">
  import {
    timeOutlist
  } from "@/api/order/order"
  const router = useRouter();
  const route = useRoute()
  const { proxy } = getCurrentInstance();
  const groupList = ref([]);
  const open = ref(false);
  const appLicationList = ref([])
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const title = ref("");
  const dateRange = ref([]);
  const menuOptions = ref([]);
  const menuExpand = ref(false);
  const menuNodeAll = ref(false);
  const deptExpand = ref(true);
  const deptNodeAll = ref(false);
  const deptOptions = ref([]);
  const openDataScope = ref(false);
  const menuRef = ref(null);
  const deptRef = ref(null);
  const data = reactive({
    form: {},
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      orderNo: '',
      orderLineNo: undefined,
      shopsId: undefined,
      orderProduceStatus: '',
    },
    rules: {

    },
  });
  const { queryParams, form, rules } = toRefs(data);
  /** 查询订单列表 */
  function getList() {
    loading.value = true;
    let formQueryParam = { ...queryParams.value };
    formQueryParam.expireTemp = formQueryParam.expireTemp == 0 ? '' : formQueryParam.expireTemp
    timeOutlist(proxy.addDateRange(formQueryParam, dateRange.value)).then(response => {
      groupList.value = response.rows
      total.value = response.total;
      loading.value = false;
    });
  }

  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }
  /** 重置按钮操作 */
  function resetQuery() {
    dateRange.value = [];
    queryParams.value = {
      pageNum: 1,
      pageSize: 10,
      orderNo: '',
      orderLineNo: undefined,
      shopsId: undefined,
      orderProduceStatus: '',
    }
    handleQuery();
  }

  /** 删除按钮操作 */
  function handleDelete(row) {
    const tenantGroupIds = row.tenantGroupId || ids.value;
    proxy.$modal.confirm('是否确认删除订单编号为"' + tenantGroupIds + '"的数据项?').then(function() {
      return delgroup(tenantGroupIds);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
  }

  /** 导出按钮操作 */
  function handleExport() {
    proxy.download("system/role/export", {
      ...queryParams.value,
    }, `role_${new Date().getTime()}.xlsx`);
  }

  /** 多选框选中数据 */
  function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.tenantGroupId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
  }

  /** 订单状态修改 */
  function handlegroupAvlChange(row) {
    let text = row.groupAvl === "0" ? "启用" : "停用";
    proxy.$modal.confirm('确认要"' + text + '""' + row.groupName + '"订单吗?').then(function() {
      return changeRolegroupAvl(row.tenantGroupId, row.groupAvl);
    }).then(() => {
      proxy.$modal.msgSuccess(text + "成功");
    }).catch(function() {
      row.groupAvl = row.groupAvl === "0" ? "1" : "0";
    });
  }

  /** 更多操作 */
  function handleCommand(command, row) {
    switch (command) {
      case "handleDataScope":
        handleDataScope(row);
        break;
      case "handleAuthUser":
        handleAuthUser(row);
        break;
      default:
        break;
    }
  }

  /** 分配用户 */
  function handleAuthUser(row) {
    router.push("/system/role-auth/user/" + row.tenantGroupId);
  }


  /** 所有部门节点数据 */
  function getDeptAllCheckedKeys() {
    // 目前被选中的部门节点
    let checkedKeys = deptRef.value.getCheckedKeys();
    // 半选中的部门节点
    let halfCheckedKeys = deptRef.value.getHalfCheckedKeys();
    checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
    return checkedKeys;
  }

  /** 重置新增的表单以及其他数据  */
  function reset() {
    if (menuRef.value != undefined) {
      menuRef.value.setCheckedKeys([]);
    }
    menuExpand.value = false;
    menuNodeAll.value = false;
    deptExpand.value = true;
    deptNodeAll.value = false;
    form.value = {
      tenantGroupName: undefined,
      tenantGroupLinkName: undefined,
      tenantGroupLinkMobile: '',
      tenantGroupExpireStatus: 'F',
      tenantGroupAvl: 'Y',
      tenantCreateParams: []
    };
    proxy.resetForm("roleRef");
  }



  /** 修改订单 */
  function handleUpdate(row) {
    reset();
    const tenantGroupId = row.tenantGroupId || ids.value;
    getgroup(tenantGroupId).then(response => {
      form.value = response.data;
      open.value = true;
      title.value = "修改订单";
    });
  }

  /** 取消按钮 */
  function cancel() {
    open.value = false;
    reset();
  }


  /** 取消按钮（数据权限）*/
  function cancelDataScope() {
    openDataScope.value = false;
    reset();
  }
  queryParams.value.expireTemp = route.query.expireTemp ? route.query.expireTemp : '';
  getList();
</script>